package com.mradio.app.web;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;

import com.mradio.app.domain.Article;
import com.mradio.app.domain.LoginUser;

@Configurable
/**
 * A central place to register application converters and formatters. 
 */
public class ApplicationConversionServiceFactoryBean extends FormattingConversionServiceFactoryBean {

	@SuppressWarnings("deprecation")
	@Override
	protected void installFormatters(FormatterRegistry registry) {
		super.installFormatters(registry);
		// Register application converters and formatters
	}

	public Converter<Article, String> getArticleToStringConverter() {
        return new Converter<com.mradio.app.domain.Article, java.lang.String>() {
            public String convert(Article article) {
                return new StringBuilder().append(article.getName()).toString();
            }
        };
    }

	public Converter<Long, Article> getIdToArticleConverter() {
        return new Converter<java.lang.Long, com.mradio.app.domain.Article>() {
            public Article convert(java.lang.Long id) {
                return Article.findArticle(id);
            }
        };
    }

	public Converter<String, Article> getStringToArticleConverter() {
        return new Converter<java.lang.String, com.mradio.app.domain.Article>() {
            public Article convert(String id) {
                return getObject().convert(getObject().convert(id, Long.class), Article.class);
            }
        };
    }

	public Converter<LoginUser, String> getLoginUserToStringConverter() {
        return new org.springframework.core.convert.converter.Converter<com.mradio.app.domain.LoginUser, java.lang.String>() {
            public String convert(LoginUser loginUser) {
                return new StringBuilder().append(loginUser.getUsername()).append(" ").append(loginUser.getPassword()).toString();
            }
        };
    }

	public Converter<Long, LoginUser> getIdToLoginUserConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.Long, com.mradio.app.domain.LoginUser>() {
            public com.mradio.app.domain.LoginUser convert(java.lang.Long id) {
                return LoginUser.findLoginUser(id);
            }
        };
    }

	public Converter<String, LoginUser> getStringToLoginUserConverter() {
        return new org.springframework.core.convert.converter.Converter<java.lang.String, com.mradio.app.domain.LoginUser>() {
            public com.mradio.app.domain.LoginUser convert(String id) {
                return getObject().convert(getObject().convert(id, Long.class), LoginUser.class);
            }
        };
    }

	public void installLabelConverters(FormatterRegistry registry) {
        registry.addConverter(getArticleToStringConverter());
        registry.addConverter(getIdToArticleConverter());
        registry.addConverter(getStringToArticleConverter());
        registry.addConverter(getLoginUserToStringConverter());
        registry.addConverter(getIdToLoginUserConverter());
        registry.addConverter(getStringToLoginUserConverter());
    }

	public void afterPropertiesSet() {
        super.afterPropertiesSet();
        installLabelConverters(getObject());
    }
}
